﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yiwoSDK;

namespace BZV.Core.Net
{
    public class QQHelper
    {
        private CMQQ cm = new CMQQ();
        private static QQHelper m_QQ;
        private string m_strQQNumber;
        private string m_strPwd;

        /// <summary>
        /// 单例模式
        /// </summary>
        /// <returns></returns>
        public static QQHelper GetInstance()
        {
            if (m_QQ == null)
            {
                m_QQ = new QQHelper();
                return m_QQ;
            }
            return m_QQ;
        }

        public void Initilize(string strQQNumber, string strPwd)
        {
            cm.RevMsg += new CMQQ.DMsg(ReceiveMessage);
            m_strQQNumber = strQQNumber;
            m_strPwd = strPwd;
            cm.Login(m_strQQNumber, m_strPwd);
        }

        public void SendQQMessage(string strToQQNumber, string strMesssage)
        {
            cm.SendMsgToFriend(strToQQNumber, strMesssage);
        }

        //有消息时会产生接收消息事件
        private void ReceiveMessage(string msg)
        {
            if (msg.IndexOf("VER=") >= 0)
            {
                string[] a = msg.Split('&');
                switch (a[1])
                {
                    case "CMD=Login"://如果返回是登陆结果信息
                        if (msg.IndexOf("&RS=0") > 0)
                            //MessageBox.Show("登陆成功！");
                            cm.GetFriendList("0");//获取好友列表
                        break;
                    case "CMD=VERIFYCODE"://如果返回验证码消息
                        if (msg.IndexOf("&VC=") > 0)
                        {
                            break;

                        }
                        break;
                    case "CMD=SIMPLEINFO2"://如果是获取好友消息
                        string NP = a[5].Substring(3);
                        if (msg.IndexOf("&NP=65535") < 0)
                        {
                            cm.GetFriendList(NP);
                        }
                        string[] QQ = a[7].Substring(3).Split(',');
                        string[] Nick = a[9].Substring(3).Split(',');
                        break;
                    case "CMD=Server_Msg"://如果返回的是聊天消息
                        string UN = a[7].Substring(3);//读取QQ
                        string MG = a[8].Substring(3);//读取消息
                        break;
                }
            }
            else
            {
                Initilize(m_strQQNumber, m_strPwd);
            }
        }
    }
}
